
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

public class JSAnalyze extends Method implements Drawable, ActionListener,
    MethodRunnable, Runnable {

	private JButton analyzeButton;

	public JSAnalyze() {
		RUNNING_STATE = "Analyzing";
		DONE_STATE = "Analyze done";
	}

	public Component drawContentPane() {
		analyzeButton = new JButton("Analyze");
		analyzeButton.setActionCommand("ANALYZE");
		analyzeButton.addActionListener(this);

		return analyzeButton;
	}

	public void setEnable(boolean enable) {
		if (analyzeButton != null)
			analyzeButton.setEnabled(enable);
	}

	// CHECK WHO SENT THE EVENT
	public void actionPerformed(ActionEvent event) {
		String command = event.getActionCommand();

		if (command.equals("ANALYZE")) {
			DataManager jsData = DataManager.getInstance();
			jsData.updateProxy();
			runMethod();
		}
	}

	protected void runAlgorithm() {
		Thread segmentationThread = new Thread(this);
		segmentationThread.start();
	}

	public void run() {
		StatusManager.getInstance().setState(RUNNING_STATE);
		MethodManager jsMethodManager = MethodManager.getInstance();
		jsMethodManager.setEnable(false);

		JSMethodProxy jsProxy = JSMethodProxy.getInstance();

		double[] mean = new double[jsProxy.getNumberOfImages()
				* jsProxy.getNumberOfRegions()];
		double[] variance = new double[jsProxy.getNumberOfImages()
				* jsProxy.getNumberOfRegions()];
		double[] curtosis = new double[jsProxy.getNumberOfImages()
				* jsProxy.getNumberOfRegions()];

		JSegmentation.analyze(jsProxy.getHeight(), jsProxy.getWidth(), jsProxy
				.getDepth(), jsProxy.getNumberOfImages(), jsProxy
				.getNumberOfRegions(), jsProxy.getImages(), jsProxy.getMask(),
				jsProxy.getInitialPoints(), mean, variance, curtosis);

		ChartManager chartManager = ChartManager.getInstance();
		chartManager.setData(jsProxy.getNumberOfImages(), jsProxy
				.getNumberOfRegions(), mean, variance, curtosis);
		chartManager.showChart();

		jsMethodManager.setEnable(true);
		StatusManager.getInstance().setState(DONE_STATE);
	}

}
